from django.shortcuts import render_to_response
from django.template import RequestContext
from functools import wraps

def render_to(tmpl, processors=None):
    """
    Decorator for views that sends returned dict to render_to_response function
    with given template and RequestContext as context instance.

    If view doesn't return dict then decorator simply returns output.
    """
    def renderer(func):
        if not hasattr(func, '__call__'):
            raise TypeError('The argument should be a callable')
        @wraps(func)
        def wrapper(request, *args, **kwargs):
            output = func(request, *args, **kwargs)
            if not isinstance(output, dict):
                return output
            context = RequestContext(request, processors=processors)
            return render_to_response(tmpl, output, context)
        return wrapper
    return renderer